/**
 * 
 */
package fpt.pits.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import fpt.pits.dto.ProjectApplicationInfo;
import fpt.pits.util.Connection;

/**
 * @author yoyosakura
 *
 */
public class ProjectApplicationInfoDAO {
	public List<ProjectApplicationInfo> getByProjectID(int projectID) {
		List<ProjectApplicationInfo> paList = null;
		ProjectApplicationInfo paInfo = null;
		java.sql.Connection cn = null;
		java.sql.PreparedStatement ps = null;
		java.sql.ResultSet rs = null;
		String query = null;
		try {
            query = "SELECT pa.* " +
            		"FROM `project_application` pa " +
            		"WHERE pa.ProjectID = ?";
            cn = Connection.getConnection();
            ps = cn.prepareStatement(query);
            ps.setInt(1, projectID);
            rs = ps.executeQuery();
            paList = new ArrayList<ProjectApplicationInfo>();
            while (rs.next()) {
                if (rs.getByte("IsDeleted") == 0) {
                	paInfo = new ProjectApplicationInfo();
                	paInfo.setProjectID(rs.getInt("ProjectID"));
                	paInfo.setAppID(rs.getInt("AppID"));
                	paInfo.setIsDeleted(rs.getByte("IsDeleted"));
                	paList.add(paInfo);
                } else {
                	return null;
                }
            }
        } catch (Exception e) {
            System.err.println("Error: Get data from database " + e.getMessage());
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (ps != null) {
                    ps.close();
                }
                if (cn != null) {
                    cn.close();
                }
            } catch (SQLException sqle) {
                System.out.println("Error: No close connection " + sqle.getMessage());
            }
        }
        if (paList.isEmpty()) {
        	return null;
        } else {
        	return paList;
        }
	}
	public List<ProjectApplicationInfo> getByAppID(int appID) {
		List<ProjectApplicationInfo> paList = null;
		ProjectApplicationInfo paInfo = null;
		java.sql.Connection cn = null;
		java.sql.PreparedStatement ps = null;
		java.sql.ResultSet rs = null;
		String query = null;
		try {
            query = "SELECT pa.* " +
            		"FROM `project_application` pa " +
            		"WHERE pa.AppID = ?";
            cn = Connection.getConnection();
            ps = cn.prepareStatement(query);
            ps.setInt(1, appID);
            rs = ps.executeQuery();
            paList = new ArrayList<ProjectApplicationInfo>();
            while (rs.next()) {
                if (rs.getByte("IsDeleted") == 0) {
                	paInfo = new ProjectApplicationInfo();
                	paInfo.setProjectID(rs.getInt("ProjectID"));
                	paInfo.setAppID(rs.getInt("AppID"));
                	paInfo.setIsDeleted(rs.getByte("IsDeleted"));
                	paList.add(paInfo);
                } else {
                	return null;
                }
            }
        } catch (Exception e) {
            System.err.println("Error: Get data from database " + e.getMessage());
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (ps != null) {
                    ps.close();
                }
                if (cn != null) {
                    cn.close();
                }
            } catch (SQLException sqle) {
                System.out.println("Error: No close connection " + sqle.getMessage());
            }
        }
        if (paList.isEmpty()) {
        	return null;
        } else {
        	return paList;
        }
	}
}
